package com.hongkuncheng.cms.helper;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 短信帮助类
 *
 * @author 洪坤成
 */
public class DuanxinHelper {

    public static String apikey;

    private static final Base64.Encoder encoder = Base64.getEncoder();

    // 发送短信
    public static boolean send(String mobile, String contents) {
        // 铁壳短信
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "Basic " + new String(encoder.encode(new String("api:" + apikey).getBytes())));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("mobile", mobile);
        params.put("message", contents + "【签名】");
        System.out.println("sms:" + params.get("message"));
        return true;
//        String json = HttpHelper.post("https://sms-api.luosimao.com/v1/send.json", headers, params);
//        JSONObject jsonObj = new JSONObject(json);
//        return jsonObj.getInt("error") == 0;
    }

    // 获取余额
    public static int balance() {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "Basic " + new String(encoder.encode(new String("api:" + apikey).getBytes())));
        String json = HttpHelper.get("http://sms-api.luosimao.com/v1/status.json", headers);
        JSONObject jsonObj = JSON.parseObject(json);
        return jsonObj.getIntValue("deposit");
    }

}
